package xiong.myiot;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class SiteAdapter extends RecyclerView.Adapter<SiteAdapter.ViewHolder> {

    private List<Site> mSiteList;

    private SiteAdapter.ImageBtnInterface imageBtnInterface;

    private SiteAdapter.ViewItemInterface viewItemInterface;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View siteView;
        ImageButton siteImageBtn;
        ImageView siteImageStatus;
        TextView siteName;
        TextView sitePosition;
        TextView siteStatus;

        public ViewHolder(View view){
            super(view);
            siteView = view;
            siteImageBtn = (ImageButton) view.findViewById(R.id.img_btn);
            siteName = (TextView) view.findViewById(R.id.site_name);
            sitePosition = (TextView) view.findViewById(R.id.site_position);
            siteStatus = (TextView) view.findViewById(R.id.site_status);
            siteImageStatus = (ImageView) view.findViewById(R.id.img_status);
        }
    }
    public SiteAdapter(List<Site>siteList){
        mSiteList = siteList;
    }

    public List<Site> getDataList() {
        return mSiteList;
    }
    /**
     * 按钮点击事件对应的接口
     */
    public interface ImageBtnInterface{
        public void onclick(View view, int position);
    }
    /**
     *按钮点击事件需要的方法
     */
    public void ImageBtnOnClick(SiteAdapter.ImageBtnInterface imageBtnInterface){
        this.imageBtnInterface = imageBtnInterface;
    }
    /**
     * Item点击事件对应的接口
     */
    public interface ViewItemInterface{
        public void onclick(View view, int position);
    }

    /**
     *item点击事件需要的方法
     */
    public void ViewItemOnClick(SiteAdapter.ViewItemInterface viewItemInterface){
        this.viewItemInterface = viewItemInterface;
    }


    @Override
    public SiteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.site_item,parent,false);
        final SiteAdapter.ViewHolder holder = new SiteAdapter.ViewHolder(view);
//        holder.siteView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                int position = holder.getAdapterPosition();
//                Site site = mSiteList.get(position);
//                Toast.makeText(v.getContext(),"you Clicked view" + site.getName(),Toast.LENGTH_LONG).show();
//
//            }
//        });
        holder.siteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if(viewItemInterface!=null) {
                    //接口实例化后的而对象，调用重写后的方法
                    viewItemInterface.onclick(v,position);
                }
            }
        });
        holder.siteImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if(imageBtnInterface!=null) {
                    //接口实例化后的而对象，调用重写后的方法
                    imageBtnInterface.onclick(v,position);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(SiteAdapter.ViewHolder holder, int position){
        Site site = mSiteList.get(position);
        holder.siteImageBtn.setImageResource(site.getImageId());
        // 标记按键设备uuid
        holder.siteImageBtn.setTag(site.getUuid());
        holder.siteImageStatus.setImageResource(site.getImageStatusId());
        holder.siteName.setText(site.getName());
        holder.siteStatus.setText(site.getStatus());
        holder.sitePosition.setText(site.getPosition());
    }

    @Override
    public int getItemCount() {
        return mSiteList.size();
    }
}
